package com.simin.mall.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by wsm on 2024/10/28.
 * <p>集合工具类
 */
public class CollectionUtil {
    public static boolean isEmpty(Collection collection) {
        if (collection == null || collection.size() == 0) {
            return true;
        }
        return false;
    }

    public static <T> boolean isArrayEmpty(T[] array) {
        if (array == null || array.length == 0) {
            return true;
        }
        return false;
    }

    public static <T> int arrayIndex(T[] source, T target) {
        if (source == null) {
            return -1;
        }
        int length = source.length;
        for (int i = 0; i < length; i++) {
            T temp = source[i];
            if (temp == null && target == null) {
                return i;
            }
            if (target != null && target.equals(temp)) {
                return i;
            }
        }
        return -1;
    }

    public static <T> List<T> reverseOrder(List<T> source) {
        if (isEmpty(source)) {
            return source;
        }
        ArrayList<T> target = new ArrayList<>();
        int size = source.size();
        for (int i = size - 1; i >= 0; i--) {
            T item = source.get(i);
            target.add(item);
        }
        return target;
    }

    public static <T> boolean contains(T[] array, T target) {
        if (array == null || array.length == 0) {
            return false;
        }
        for (T t : array) {
            if (t == null && target == null) {
                return true;
            }
            if (t != null && t.equals(target)) {
                return true;
            }
        }
        return false;
    }
}
